<?php
namespace app\index\controller;
use think\Config;
use think\Db;

class Address extends Common
{
	
	//详情
    public function index()
    {
		$baseurl=Config::get("block_url");
		$address=request()->param('address');
		
		if(strlen($address)==6)
		{
			$this->redirect("Address/Miner",['address'=>$address]);
		}
		
		
		
		//echo $address;exit;
		
		$base=array();
		$base['address']=$address;
		
		$url = $baseurl . "v1/wallet/index/info";
		
		$arr['address']=$address;
		$info = $this->curl_request($url,$arr);
		$cinfo = json_decode($info,true);
		$uinfo = $cinfo['data'];
		$uinfo['balance'] = $this->sctonum($uinfo['balance'] / 1000000000000000000);
		
		$base['balance'] = $uinfo['balance'];
		
		//最新交易
		$page=request()->param('page');
		if(!$page | $page<1)
		{
			$page = 1;
		}
		$url = $baseurl . "v1/wallet/index/user_message";
		$arr['page']=$page;
		$arr['limit']=20;
		$arr['address']=$address;
		$info = $this->curl_request($url,$arr);
		
		$cinfo = json_decode($info,true);
		
		//var_dump($cinfo);exit;
		
		
		if(!empty($cinfo['data']))
		{
			foreach($cinfo['data'] AS &$c)
			{
				$c['timestamp'] = date("Y-m-d H:i:s",$c['timestamp']);
				$c['Value'] = $this->sctonum($c['Value'] / 1000000000000000000);
				
				
				//print_r($c);
			}
			unset($c);
		}
		
		
		$page_arr=array();
		$page_arr['this']=$page;
		$page_arr['next']=$page+1;
		$page_arr['pre']=$page-1;
		$page_arr['next_last']=ceil($cinfo['echo']/20);
		
		$this->assign("page",$page_arr);
		$this->assign("data",$cinfo['data']);
		
		
		
		$this->assign("info",$base);
		
		return $this->fetch();
    }
	
	//矿工详情
	public function miner(){
		
		$baseurl=Config::get("block_url");
		$address=request()->param('address');
		
		$base=array();
		$base['address']=$address;
		
		$url = $baseurl . "v1/miner/pleg/get";
		$arr['address'] = $address;
		$info = $this->curl_request($url,$arr);
		$cinfo = json_decode($info,true);
		$updata = $cinfo['data'];		
		
		
		
		
		$url = $baseurl . "v1/miner/info/get";
		
		$arr['address']=$address;
		$info = $this->curl_request($url,$arr);
		$cinfo = json_decode($info,true);
		$uinfo = $cinfo['data'];
		$uinfo['address'] = $address;
		$uinfo['after_amount']=$this->chk_num($updata['after_amount']);
		$uinfo['lock_amount']=$this->chk_num($updata['lock_amount']);
		$uinfo['pleg_amount']=$this->chk_num($updata['pleg_amount']);
		$uinfo['amount'] = $this->chk_num($uinfo['amount']);
		$uinfo['QualityAdjPower'] = $this->chk_power($uinfo['QualityAdjPower']);
		$uinfo['RawBytePower'] = $this->chk_power($uinfo['RawBytePower']);
		$uinfo['sign_time'] = date("Y-m-d H:i:s",time());
		$uinfo['sign_code'] = "lotus wallet sign ".$uinfo['address'];
		
		if($uinfo['SectorSize'] == 0)
		{
			$uinfo['SectorSize'] = "N/A";
		}
		
		//$uinfo['blocks_num'] = $uinfo['blocks_num'] / 1000000000000000000;
		
		//var_dump($uinfo);exit;
		
		$url = $baseurl . "v1/miner/static/24";
		
		$arr['address']=$address;
		$info = $this->curl_request($url,$arr);
		$cinfo = json_decode($info,true);
		$minfo = $cinfo['data'];
		
		//var_dump($minfo);exit;
		
		$minfo['totalRewards'] = $this->sctonum($minfo['totalRewards'] / 1000000000000000000);
		
		$minfo['qualityAdjPowerGrowth'] = $this->chk_power($minfo['qualityAdjPowerGrowth']);
		
		
		
		$minfo['rawBytePowerGrowth'] = $this->chk_power($minfo['rawBytePowerGrowth']);
		$minfo['qualityAdjPowerDelta'] = $this->chk_power($minfo['qualityAdjPowerDelta']);
		
		
		//var_dump($minfo);
		
		
		//最新交易
		$page=request()->param('page');
		if(!$page | $page<1)
		{
			$page = 1;
		}
		$url = $baseurl . "v1/wallet/index/user_message";
		$arr['page']=$page;
		$arr['limit']=20;
		$arr['address']=$address;
		$info = $this->curl_request($url,$arr);
		$cinfo = json_decode($info,true);
		
		//var_dump($cinfo);exit;
		
		
		if(!empty($cinfo['data']))
		{
			foreach($cinfo['data'] AS &$c)
			{
				$c['timestamp'] = date("Y-m-d H:i:s",$c['timestamp']);
				
				//$c['Value'] = $this->chk_num($c['Value']);
				
				$c['Value'] = $this->sctonum($c['Value'] / 1000000000000000000);
				
				
				//print_r($c);
			}
			unset($c);
		}
		
		
		$page_arr=array();
		$page_arr['this']=$page;
		$page_arr['next']=$page+1;
		$page_arr['pre']=$page-1;
		$page_arr['next_last']=ceil($cinfo['echo']/20);
		
		$this->assign("page",$page_arr);
		$this->assign("data",$cinfo['data']);
		
		
		$this->assign("uinfo",$uinfo);
		$this->assign("minfo",$minfo);
		
		
		return $this->fetch();
	}
	
	
	
	//签名提交
	public function usign(){
		$owner_address=request()->param('owner_address');
		$accname_en=request()->param('accname_en');
		$accname_zh=request()->param('accname_zh');
		$contact=request()->param('contact');
		$signature=request()->param('signature');
		
		$arr=array();
		
		
		
		//echo "{$owner_address}\n{$accname_en}\n{$accname_zh}\n{$contact}\n{$signature}";exit;
		
		if(!$owner_address | !$accname_en | !$accname_zh | !$contact | $signature)
		{
			$arr['code'] = -1;
			$arr['msg'] = "Please confirm whether the parameters are correct.";
			echo json_encode($arr);
			//exit;
		}
		
		
		$ip = $_SERVER['REMOTE_ADDR'];
		$time = time();
		
		$sql="INSERT INTO usign(owner_address,accname_en,accname_zh,contact,signature,ip,create_time) VALUES('{$owner_address}','{$accname_en}','{$accname_zh}','{$contact}','{$signature}','{$ip}','{$time}')";
		Db::execute($sql);
		
		
		
		$arr['code'] = 0;
		$arr['msg'] = "Submitted successfully, please wait for review.";
		
		echo json_encode($arr);
		
	}
	
}
